<?php
namespace app\admin\controller;
use think\Db;
class Seller extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = request()->post();
            $map = [];
            if(!empty($d['status'])){
                $map[] = ['status','=',$d['status']];
            }
            if(!empty($d['company_id'])){
                $map[] = ['company_id','=',$d['company_id']];
            }
            if(!empty($d['keyword'])){
                $map[] = ['name|phone','like',"%{$d['keyword']}%"];
            }
        	$rs = model('seller')::where($map)
                                ->withCount('shop')
                                ->order('id desc')
                                ->paginate($d['limit'],false,['page'=>$d['page']]);
            if($rs){
                $rs = $rs->toArray();
                return suc($rs['data'], $rs['total']);
            }else{
                return suc([],0);
            }
        }
        $rs = model('company')::where('status',0)->order('id desc')->select();
        $list = $rs ? $rs->toArray() : [];
        return $this->fetch('',['company'=>$list]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = request()->post();
            $rule = [
                'password'=>'require|min:6',
            ];
            $msg = [];
            $r = $this->validate($d, $rule,$msg);
            if($r!==true) return err($r);
            $d['password'] = com_password($d['password']);
            if(model('Seller')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('Seller')->getError());
            }
        }
        $rs = model('company')::where('status',0)->order('id desc')->select();
        $company = $rs ? $rs->toArray() : [];
        return $this->fetch('', ['company'=>$company]);
    }

    //编辑
    public function edit($id=''){
        if(is_post()){
            $d = request()->post();
            $rule = [
                'password'=>'min:6',
            ];
            $msg = [];
            $r = $this->validate($d, $rule,$msg);
            if($r!==true){
                return err($r);
            }
            if(!empty($d['password'])){
                $d['password'] = com_password($d['password']);
            }
            if(model('Seller')->renew($d)){
                session('js.function','preload');
                return suc();
            }else{
                return err(model('Seller')->getError());
            }
        }
        $info = model('seller')::get($id);
        $rs = model('company')::where('status',0)->order('id desc')->select();
        $company = $rs ? $rs->toArray() : [];
        return $this->fetch('', ['info'=>$info,'company'=>$company]);
    }
    //删除
    public function del(){
        if(is_post()){
            $rs = model('seller')::get(input('id'));
            if($rs && $rs->getData('status')==2){
                $rs->delete();
                session('js.function','reload');
                return suc();
            }else{
                return err('只有冻结的商户可以删除');
            }
        }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
